﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_QueryFrom_frm_SetDeptByArea : System.Web.UI.Page
{
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdQueryResult.CurrentPageIndex;
            int intPageCounts = this.dgrdQueryResult.PageCount;

            //判断翻页方向
            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            //this.BindQueryDataSet(dst, intPageIndex);
        }
    }

    private void GetDeptDataSetByArea(string AreaNo)
    {
        ClsAreaSetDAL cls_AreaSet = new ClsAreaSetDAL();
        DataSet dst = cls_AreaSet.GetDeptDataSetByArea(AreaNo);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();

            cls_Property.SetOrGetQueryDataSet = dst;

            this.BindQueryDataSet(dst, 0);
        }
        else
        {
            this.dgrdQueryResult.CurrentPageIndex = 0;
            this.dgrdQueryResult.DataSource = null;
            this.dgrdQueryResult.DataBind();
            this.lblQueryRsCounts.Text = "共0条记录";
            this.lblQueryPageCounts.Text = "共0页";
            this.lblQueryCurrPage.Text = "当前第0页";
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(172, 168, 153);
        }
    }

    private void BindQueryDataSet(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdQueryResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdQueryResult.DataSource = dst;
            this.dgrdQueryResult.DataBind();

            int intPageCount = this.dgrdQueryResult.PageCount;
            int intCurrPage = this.dgrdQueryResult.CurrentPageIndex + 1;
            this.lblQueryRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblQueryPageCounts.Text = "共" + intPageCount + "页";
            this.lblQueryCurrPage.Text = "当前第" + intCurrPage + "页";
            this.lblQueryRsCounts.Visible = this.lblQueryPageCounts.Visible = this.lblQueryCurrPage.Visible = true;
            this.lbtnQueryFirst.Visible = this.lbtnQueryPrev.Visible = this.lbtnQueryNext.Visible = this.lbtnQueryEnd.Visible = true;
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = true;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(78, 78, 78);

            if (intPageCount == 1)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage == intPageCount)
            {
                this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage - 1 == 0)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strAreaNo = this.Page.Request.Params["AreaNo"];
            this.hidAreaNO.Value = strAreaNo;

            if (strAreaNo != null && strAreaNo.Trim() != "")
            {
                this.GetDeptDataSetByArea(strAreaNo);
            }
        }
        else
        {
            this.lblJs.Text = "";
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        string strAreaNo = this.hidAreaNO.Value.Trim();

        if (strAreaNo != "")
        {
            this.GetDeptDataSetByArea(strAreaNo);
        }
    }

    protected void DeleteDeptByArea(object sender, ImageClickEventArgs e)
    { 
        int intID = Int32.Parse(((ImageButton)sender).CommandName);

        ClsAreaSetDAL cls_AreaSet = new ClsAreaSetDAL();
        Boolean is_succeed = cls_AreaSet.DeleteDeptSetByArea(intID);

        if (is_succeed)
        {
            //从新绑定数据
            this.ibtnRef_Click(null, null);
        }
        else
        {
            this.lblJs.Text = "<script>alert('作废地区部门失败，请重试！');</script>";
        }
    }

    protected void ibtnServer_Click(object sender, ImageClickEventArgs e)
    {
        string strDeptNo = this.txtDeptNo.Text.Trim();

        if (strDeptNo != "")
        {
            string strAreaNo = this.hidAreaNO.Value.Trim();

            if (strAreaNo != "")
            {
                ClsAreaSetDAL cls_AreaSet = new ClsAreaSetDAL();
                Boolean is_succeed = cls_AreaSet.AddDeptSetByArea(strAreaNo, strDeptNo);

                if (is_succeed)
                {
                    //从新绑定数据
                    this.ibtnRef_Click(null, null);

                    this.txtDeptNo.Text = "";
                }
                else
                {
                    this.lblJs.Text = "<script>alert('新增地区部门失败，请重试！');</script>";
                }
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('部门不能为空！');</script>";
        }
    }
}
